package com.hogwarts.webui;

import com.hogwarts.base.WebUIBase;
import io.qameta.allure.Feature;
import io.qameta.allure.Story;
import org.apache.log4j.Logger;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

@Feature("WebUI上传文件")
public class WebUITestUploadFile extends WebUIBase {
    private Logger logger = Logger.getLogger(WebUITestUploadFile.class);

    @Test
    @Story("文件上传")
    public void UploadFile() {
        logger.info("开始 WebUITestUploadFile 的测试");

        String url = "https://the-internet.herokuapp.com/upload";
        logger.info("打开 " + url);
        navigation.to(url);

        String fileFullPath = projRootPath + "/files/sample.pdf";
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30l));
        WebElement fileUploadInput = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("file-upload")));
        wait5s();
        fileUploadInput.sendKeys(fileFullPath);
        wait5s();

        logger.info("单击上传按钮");
        WebElement submitBtn = wait.until(ExpectedConditions.elementToBeClickable(By.id("file-submit")));
        submitBtn.click();
        wait5s();

        //校验上传结果
        WebElement fileUploadResultBox = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("uploaded-files")));
        boolean ret = fileUploadResultBox.getText().trim().equalsIgnoreCase("sample.pdf");
        logger.info("校验上传结果正确性 ==> " + ret);
        Assertions.assertTrue(ret, "期望结果没有找到，上传错误！");
        wait1s();
    }

}
